<?php

require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/shared/db_connection.php';

function add_journal($journal_id, $title, $thumb, $short_description, $printISSN, $onlineISSN, $publish, $photo, $creationDate){
    $con = open_connect();
    //run the store proc
    echo "CALL addJournal(".$journal_id.",'".$title."','".$thumb."','".$short_description."','".$printISSN."','".$onlineISSN."','".$publish."','".$photo."','".$creationDate."')";
    $result = mysqli_query($con, "CALL addJournal(".$journal_id.",'".$title."','".$thumb."','".$short_description."','".$printISSN."','".$onlineISSN."','".$publish."','".$photo."','".$creationDate."')")
            or die("Query fail: " . mysqli_error());
    close_connect($con);

}
function getJournal($journal_id) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetJournalsById(".$journal_id.")") 
            or die("Query fail: " . mysqli_error());

    $row = mysqli_fetch_array($result);
    
    $item = array();
    $item["JournalID"] = $journal_id;
    $item["Title"] = $row['Title'];
    $item["Thumb"] = $row['Thumb'];
    $item["ShortDescription"] = $row['ShortDescription'];
    $item["PrintISSN"] = $row['PrintISSN'];
    $item["OnlineISSN"] = $row['OnlineISSN'];
    $item["Publish"] = $row['Publish'];
    $item["Photo"] = $row['Photo'];
    $item["CreationDate"] = $row['CreationDate'];
    
    close_connect($con);
    return $item;
}

function deleteJournal($journal_id) {
    $con = open_connect();
    //run the store proc

    mysqli_query($con, "DELETE FROM tblcategories WHERE JournalID=".$journal_id) 
            or die("Query fail: " . mysqli_error());
    mysqli_query($con, "DELETE FROM tbljournals WHERE JournalID=".$journal_id) 
            or die("Query fail: " . mysqli_error());

    close_connect($con);
}

function getAllJournals() {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournals()") 
            or die("Query fail: " . mysqli_error());
    
    $list = array();
    $count = 0;
    while($row = mysqli_fetch_array($result))
    {
            $item = array();
            $item["JournalID"] = $row['JournalID'];
            $item["Title"] = $row['Title'];
            $item["Thumb"] = $row['Thumb'];
            $item["ShortDescription"] = $row['ShortDescription'];
            $item["PrintISSN"] = $row['PrintISSN'];
            $item["OnlineISSN"] = $row['OnlineISSN'];
            $item["Publish"] = $row['Publish'];
            $item["Photo"] = $row['Photo'];
            $item["CreationDate"] = $row['CreationDate'];
            $list[$count] = $item;
            $count = $count + 1;
    }
    
    close_connect($con);
    return $list;
}

function getJournalPaging($page) {
    $con = open_connect();
    //run the store proc
    $result = mysqli_query($con, "CALL GetAllJournals()") 
            or die("Query fail: " . mysqli_error());
    
    $list = array();
    $count = 0;
    while($row = mysqli_fetch_array($result))
    {
            $item = array();
            $item["JournalID"] = $row['JournalID'];
            $item["Title"] = $row['Title'];
            $item["Thumb"] = $row['Thumb'];
            $item["ShortDescription"] = $row['ShortDescription'];
            $item["PrintISSN"] = $row['PrintISSN'];
            $item["OnlineISSN"] = $row['OnlineISSN'];
            $item["Publish"] = $row['Publish'];
            $item["Photo"] = $row['Photo'];
            $item["CreationDate"] = $row['CreationDate'];
            $list[$count] = $item;
            $count = $count + 1;
    }
    
    $item_per_page = 10;
    $paging = array();
    $paging['current_page'] = $page;
    $paging['list'] = $list;
    $paging['item_total'] = $count;
    $paging['page_total'] = $count / $item_per_page;
    
    close_connect($con);
    return $list;
}